<?php  
class ControllerModulecategorynews extends Controller {
	protected $category_news_id = 0;
	protected $path_news = array();
	
	protected function index() {
		$this->language->load('module/category_news');
		
    	$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->load->model('catalog/category_news');
		$this->load->model('catalog/news');
		$this->load->model('tool/seo_url');
		
		if(isset($this->request->get['_route_'])){
			$link_news = explode('/', $this->request->get['_route_']);
			if ($link_news[0] == "news"){
			$this->data['news'] = 1;
			} else {
			$this->data['news'] = 0;
			}
		} else {
			$this->data['news'] = 0;
		}
		$path_news = 0;
		if (isset($this->request->get['path_news'])) {
			$category_news_info = $this->model_catalog_category_news->getCategory($this->request->get['path_news']);
			if($category_news_info) {
			$path_news = $this->request->get['path_news'];
			while ($category_news_info['parent_news_id'] != 0) {
				$path_news = $category_news_info['parent_news_id'] . '_' . $path_news;
				$category_news_info = $this->model_catalog_category_news->getCategory($category_news_info['parent_news_id']);
			}
			}
			$this->category_news_id = $this->request->get['path_news'];
		}
		
		if (isset($this->request->get['news_id'])) {
			$news_id = $this->request->get['news_id'];
		} else {
			$news_id = 0;
		}
		
		$news_info = $this->model_catalog_news->getnews($news_id);
   		
		if ($news_info) {
			if ($news_info['category_news_id']) {
				$category_news_info = $this->model_catalog_category_news->getcategory($news_info['category_news_id']);
				if($category_news_info) {
				$path_news = $news_info['category_news_id'];
				while ($category_news_info['parent_news_id'] != 0) {
					$path_news = $category_news_info['parent_news_id'] . '_' . $path_news;
					$category_news_info = $this->model_catalog_category_news->getcategory($category_news_info['parent_news_id']);
				}
				}
				$this->category_news_id = $news_info['category_news_id'];
			}
		}
		
		$this->path_news = explode('_', $path_news);
		
		$this->data['category_news'] = $this->getCategories(0);
												
		$this->id = 'category_news';

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category_news.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/module/category_news.tpl';
		} else {
			$this->template = 'default/template/module/category_news.tpl';
		}
		
		$this->render();
  	}
	
	protected function getCategories($parent_news_id, $current_path_news = '') {
		$category_news_id = array_shift($this->path_news);
		
		$output = '';
		
		$results = $this->model_catalog_category_news->getCategories($parent_news_id);
		
		if ($results) {
			if(!$current_path_news){
				$output .= '<ul>';
			} else {
				$output .= '<ul class="child">';
			}
    	}
		
		foreach ($results as $result) {
			$new_path_news = $result['category_news_id'];
			
			$children = '';

			if ($this->category_news_id == $result['category_news_id']) {
				$select = ' class="select"';
			} else {
				$select = '';
			}
			
			$output .= '<li' . $select . '>';
			
				if ($category_news_id == $result['category_news_id']) {
					$children = $this->getCategories($result['category_news_id'], $new_path_news);
				}
				$output .= '<a ' . $select . 'href="' . $this->model_tool_seo_url->rewrite($this->url->http('product/category_news&path_news=' . $new_path_news))  . '">' . $result['name'] . '</a>';

        	$output .= $children;
        
        	$output .= '</li>'; 
		}
 
		if ($results) {
			$output .= '</ul>';
		}
		
		return $output;
	}		
}
?>